package com.test.springmvc;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.jni.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import DB.Rijidb;
import DB.Userdb;
import Json.AbstractJsonObject;
import Json.JackJsonUtils;
import Json.ListObject;
import Json.StatusCode;
import Utils.ResponseUtils;
import bean.Rijibean;
import bean.Userbean;
import jdk.nashorn.api.scripting.AbstractJSObject;

@Controller
public class Show {
	@ResponseBody
	@RequestMapping(value="/login")
	public void welcome(HttpServletRequest reqest,HttpServletResponse rep) {
		String username=reqest.getParameter("username");
		String pass=reqest.getParameter("password");
			List<Userbean> list = Userdb.getAllStudents();
			String password=Userdb.getpasswor(username);
			System.out.println("打印"+username+"?"+password+"?"+pass);
			for(int i=0;i<list.size();i++) {
				if (username.equals(list.get(i).getName())) {
					if(pass.equals(password)) {
						ListObject listObject = new ListObject();
						listObject.setItems(list);
						listObject.setCode(StatusCode.CODE_SUCCESS);
						listObject.setMsg("登录成功");
						ResponseUtils.renderJson(rep, JackJsonUtils.toJson(listObject));
					}else {
						ListObject listObject = new ListObject();
						listObject.setCode(StatusCode.CODE_ERROR);
						listObject.setMsg("密码错误");
						ResponseUtils.renderJson(rep, JackJsonUtils.toJson(listObject));
					}
					break;
				}else {
					ListObject listObject = new ListObject();
					listObject.setCode(StatusCode.CODE_ERROR);
					listObject.setMsg("登录失败");
					ResponseUtils.renderJson(rep, JackJsonUtils.toJson(listObject));  
					break;
			}
				
		}
	}
	//修改用户名
	@ResponseBody
	@RequestMapping(value="/update")
	public void update(HttpServletRequest reqest,HttpServletResponse rep) {
		try {
			reqest.setCharacterEncoding("UTF-8");
			rep.setContentType("text/html;charset=UTF-8");
			String username=reqest.getParameter("username");
			int i=Userdb.update(username);
			ResponseUtils.render(rep,"text/plain;charset=UTF-8",i+"");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      
	}
	//获取日记列表
	@ResponseBody
	@RequestMapping(value="/riji")
		public void getallrijilist(HttpServletRequest reqest,HttpServletResponse rep) {
		List<Rijibean> list = Rijidb.getrijilist();
		ListObject listObject = new ListObject();
		listObject.setItems(list);
		listObject.setCode(StatusCode.CODE_SUCCESS);
		listObject.setMsg("请求成功");
		ResponseUtils.renderJson(rep, JackJsonUtils.toJson(listObject));
	}	
	
	//新增日记
	@ResponseBody
	@RequestMapping(value="/rijiadd")
	public void insertriji(HttpServletRequest reqest,HttpServletResponse rep) throws UnsupportedEncodingException {
		reqest.setCharacterEncoding("UTF-8");
		rep.setContentType("text/html;charset=UTF-8");
		String title=reqest.getParameter("title");
		String content=reqest.getParameter("content");
		String time=reqest.getParameter("time");
		int i=Rijidb.insert(title,
				content,time);
		AbstractJsonObject aaa=new AbstractJsonObject();
		if(i==1) {
			aaa.setCode(StatusCode.CODE_SUCCESS);
			aaa.setMsg("添加成功");
		}else {
			aaa.setCode(StatusCode.CODE_ERROR);
			aaa.setMsg("添加失败");
		}
		ResponseUtils.renderJson(rep, JackJsonUtils.toJson(aaa));
	}
	
	//上传文件
	@ResponseBody
	@RequestMapping(value="/addfile")
	public void upload(HttpServletRequest request,
            @RequestParam("description") String description,
            @RequestParam("file") MultipartFile file) {
		    System.out.println(description);
		    String ss=file.getOriginalFilename();
		    System.out.println("名称:"+ss);
	}
	
	
}

